﻿// <copyright file="CharacterAddEditOwner.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Windows.Controls;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class CharacterAddEditOwner
    {
        partial void CharacterAddEditOwner_InitializeDataWorkspace(List<Microsoft.LightSwitch.IDataService> saveChangesTo)
        {
            if (this.CharacterId == null)
            {
                this.Character = new Character();
                this.Character.State = (int)CharacterStateEnum.Created;
                this.Character.Visibility = (int)VisibilityEnum.Public;
                this.Character.Owner = this.CurrentPlayer.First();

                this.DisplayName = "Add new Character";
            }
            else
            {
                this.Character = this.CharacterQuery;
                this.DisplayName = string.Format("Edit {0}", this.Character.Name);

                // When a character is approved or submitted nearly all fields are made readonly
                if (this.Character.State == (int)CharacterStateEnum.Approved ||
                    this.Character.State == (int)CharacterStateEnum.Submitted)
                {
                    this.FindControl("Character_Visibility").IsEnabled = false;
                    this.FindControl("Character_Name").IsEnabled = false;
                    this.FindControl("Character_Gender").IsEnabled = false;
                    this.FindControl("Character_Race").IsEnabled = false;
                    this.FindControl("Character_Alignment").IsEnabled = false;
                    this.FindControl("Character_Faith").IsEnabled = false;
                    this.FindControl("Character_BloodlineDerivation").IsEnabled = false;
                    this.FindControl("Character_BloodlineStrength").IsEnabled = false;
                    this.FindControl("Character_BloodlineScore").IsEnabled = false;

                    this.FindControl("RelationCharacterBloodAbilitiesAddAndEditNew").IsEnabled = false;
                    this.FindControl("RelationCharacterBloodAbilities_DeleteSelected").IsEnabled = false;

                    this.FindControl("RelationCharacterClassesAddAndEditNew").IsEnabled = false;
                    this.FindControl("RelationCharacterClasses_DeleteSelected").IsEnabled = false;

                    this.FindControl("RelationCharacterSkillsAddAndEditNew").IsEnabled = false;
                    this.FindControl("RelationCharacterSkills_DeleteSelected").IsEnabled = false;

                    this.FindControl("Character_LieutenantOf").IsEnabled = false;
                    this.FindControl("Character_Army").IsEnabled = false;
                }

                // When submitted, everything is readonly
                if (this.Character.State == (int)CharacterStateEnum.Submitted)
                {
                    this.FindControl("Character_Portrait").IsEnabled = false;
                    this.FindControl("Character_PublicInformation").IsEnabled = false;
                    this.FindControl("Character_OwnerInformation").IsEnabled = false;
                }
            }
        }

        #region Submission to GMs

        partial void SubmitToGMs_CanExecute(ref bool result)
        {
            result = this.Character.State == (int)CharacterStateEnum.Created ||
                     this.Character.State == (int)CharacterStateEnum.Rejected;
        }

        partial void SubmitToGMs_Execute()
        {
            // Try to save to see if there are problems with the Character
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.Character.State = (int)CharacterStateEnum.Submitted;
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.Close(false);
        }

        #endregion

        #region Character Sheet

        partial void UploadCharacterSheet_CanExecute(ref bool result)
        {
            result = this.Character.State != (int)CharacterStateEnum.Submitted;
        }

        partial void UploadCharacterSheet_Execute()
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
            {
                OpenFileDialog dialog = new OpenFileDialog();
                if (dialog.ShowDialog() == true)
                {
                    this.Character.CharacterSheetFilename = dialog.File.Name;
                    using (FileStream stream = dialog.File.OpenRead())
                    {
                        byte[] buffer = new byte[dialog.File.Length];
                        stream.Read(buffer, 0, (int)dialog.File.Length);
                        this.Character.CharacterSheetData = buffer;
                    }
                }
            });
        }

        partial void DownloadCharacterSheet_CanExecute(ref bool result)
        {
            result = this.Character.CharacterSheetData != null;
        }

        partial void DownloadCharacterSheet_Execute()
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.DefaultExt = Path.GetExtension(this.Character.CharacterSheetFilename);
                dialog.Filter = string.Format("Character sheet (*{0})|*{0}", Path.GetExtension(this.Character.CharacterSheetFilename));
                if (dialog.ShowDialog() == true)
                {
                    using (Stream file = dialog.OpenFile())
                    {
                        file.Write(this.Character.CharacterSheetData, 0, this.Character.CharacterSheetData.Length);
                    }
                }
            });
        }

        #endregion

        #region Add BloodAbility

        partial void RelationCharacterBloodAbilitiesAddAndEditNew_Execute()
        {
            this.RelationCharacterBloodAbilities.AddNew();

            this.FindControl("ModalWindowBloodAbility").DisplayName = "Add new Blood Ability";
            this.OpenModalWindow("ModalWindowBloodAbility");
        }

        partial void SubmitModalBloodAbility_Execute()
        {
            this.CloseModalWindow("ModalWindowBloodAbility");
        }

        partial void CancelModalBloodAbility_Execute()
        {
            this.CloseModalWindow("ModalWindowBloodAbility");
            this.RelationCharacterBloodAbilities.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add Class

        partial void RelationCharacterClassesAddAndEditNew_Execute()
        {
            this.RelationCharacterClasses.AddNew();

            this.FindControl("ModalWindowClass").DisplayName = "Add new Class";
            this.OpenModalWindow("ModalWindowClass");
        }

        partial void SubmitModalClass_Execute()
        {
            this.CloseModalWindow("ModalWindowClass");
        }

        partial void CancelModalClass_Execute()
        {
            this.CloseModalWindow("ModalWindowClass");
            this.RelationCharacterClasses.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add Skill

        partial void RelationCharacterSkillsAddAndEditNew_Execute()
        {
            this.RelationCharacterSkills.AddNew();

            this.FindControl("ModalWindowSkill").DisplayName = "Add new Skill";
            this.OpenModalWindow("ModalWindowSkill");
        }

        partial void SubmitModalSkill_Execute()
        {
            this.CloseModalWindow("ModalWindowSkill");
        }

        partial void CancelModalSkill_Execute()
        {
            this.CloseModalWindow("ModalWindowSkill");
            this.RelationCharacterSkills.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region View Domain

        partial void ViewDetailsDomain_CanExecute(ref bool result)
        {
            result = this.Domains.SelectedItem != null;
        }

        partial void ViewDetailsDomain_Execute()
        {
            this.Application.ShowDomainViewOwner(this.Domains.SelectedItem.Id);
        }

        #endregion

        #region View Lieutenant

        partial void ViewLieutenantDetails_CanExecute(ref bool result)
        {
            result = this.Lieutenants.SelectedItem != null;
        }

        partial void ViewLieutenantDetails_Execute()
        {
            this.Application.ShowCharacterAddEditOwner(this.Lieutenants.SelectedItem.Id);
        }

        #endregion
    }
}
